package cn.qingyun.gis.modules.alongstreet.controller;

import cn.qingyun.gis.common.Result;
import cn.qingyun.gis.modules.alongstreet.entity.AlongStreetProofread;
import cn.qingyun.gis.modules.alongstreet.request.StreetAddRequest;
import cn.qingyun.gis.modules.alongstreet.request.StreetKwRequest;
import cn.qingyun.gis.modules.alongstreet.request.StreetRequest;
import cn.qingyun.gis.modules.alongstreet.service.IAlongStreetProofreadService;
import cn.qingyun.gis.modules.init.system.annotation.AutoLog;
import cn.qingyun.gis.modules.init.system.base.controller.BasicController;
import cn.qingyun.gis.modules.init.system.query.QueryGenerator;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;
import java.util.Arrays;


/**
 * @Description: 表后台管理
 * @Version: V1.0
 */
@Api(tags = "")
@RestController
@RequestMapping("/admin/along-street-proofread")
@Slf4j
public class AlongStreetProofreadController extends BasicController<AlongStreetProofread, IAlongStreetProofreadService> {
    @Resource
    private IAlongStreetProofreadService iAlongStreetProofreadService;

    /**
     * 分页列表查询
     *
     * @param alongStreetProofread
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "表-分页列表查询")
    @ApiOperation(value = "表-分页列表查询", notes = "表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(AlongStreetProofread alongStreetProofread,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<AlongStreetProofread> queryWrapper = QueryGenerator.initQueryWrapper(alongStreetProofread, req.getParameterMap());
        Page<AlongStreetProofread> page = new Page<>(pageNo, pageSize);
        IPage<AlongStreetProofread> pageList = iAlongStreetProofreadService.page(page, queryWrapper);
        return Result.okData(pageList);
    }

    /**
     * 添加
     *
     * @param alongStreetProofread
     * @return
     */
    @AutoLog(value = "表-添加")
    @ApiOperation(value = "表-添加", notes = "表-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody AlongStreetProofread alongStreetProofread) {
        iAlongStreetProofreadService.save(alongStreetProofread);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param alongStreetProofread
     * @return
     */
    @AutoLog(value = "表-编辑")
    @ApiOperation(value = "表-编辑", notes = "表-编辑")
    @PostMapping(value = "/edit")
    public Result<?> edit(@RequestBody AlongStreetProofread alongStreetProofread) {
        iAlongStreetProofreadService.updateById(alongStreetProofread);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "表-通过id删除")
    @ApiOperation(value = "表-通过id删除", notes = "表-通过id删除")
    @GetMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id") String id) {
        iAlongStreetProofreadService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "表-批量删除")
    @ApiOperation(value = "表-批量删除", notes = "表-批量删除")
    @GetMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids") String ids) {
        this.iAlongStreetProofreadService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "表-通过id查询")
    @ApiOperation(value = "表-通过id查询", notes = "表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id") String id) {
        AlongStreetProofread alongStreetProofread = iAlongStreetProofreadService.getById(id);
        if (alongStreetProofread == null) {
            return Result.error("未找到对应数据");
        }
        return Result.okData(alongStreetProofread);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param alongStreetProofread
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, AlongStreetProofread alongStreetProofread) {
        return super.exportXls(request, alongStreetProofread, AlongStreetProofread.class, "表");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, AlongStreetProofread.class);
    }

    @GetMapping("/query")
    @ApiOperation(value = "沿街POI列表查询")
    public Result<?> queryPoiByParams(@Validated StreetRequest request) {
        return Result.okData(iAlongStreetProofreadService.queryPoiByParams(request));
    }

    @PostMapping("/importPoiCsv")
    @ApiOperation(value = "导入csv格式沿街POI数据")
    public Result<?> importPoiCsv(@RequestParam("file") MultipartFile file) {
        iAlongStreetProofreadService.importPoiCsv(file);
        return Result.ok("正在导入沿街POI, 请稍后");
    }

    @GetMapping("/deleteStreet")
    @ApiOperation(value = "格式化沿街POI")
    public Result<?> deleteStreet() {
        iAlongStreetProofreadService.remove(null);
        return Result.ok("正在格式化沿街POI, 请稍后");
    }

    @PostMapping("/proofread")
    @ApiOperation("沿街POI勘误")
    public Result<?> proofread(@RequestBody @Validated StreetKwRequest request) {
        return Result.okData(iAlongStreetProofreadService.streetProofread(request));
    }


    @ApiOperation(value = "沿街摸排表-添加", notes = "沿街摸排表-添加")
    @PostMapping(value = "/addStreet")
    public Result<?> addStreet(@RequestBody StreetAddRequest request) {
        iAlongStreetProofreadService.addStreet(request);
        return Result.ok("添加成功！");
    }

    @GetMapping("/image/{imagePath}/{imageName}")
    @ApiOperation(value = "图片下载")
    public void downloadImage(HttpServletResponse response,
                              @PathVariable(value = "imagePath", required = false) @Validated @NotBlank(message = "图片路径不能为空！") String imagePath,
                              @PathVariable(value = "imageName", required = false) @Validated @NotBlank(message = "图片名称不能为空！") String imageName) {

        iAlongStreetProofreadService.downloadImage(response, String.format("%s/%s", imagePath, imageName));
    }
}
